package com.amex.uam.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.amex.uam.model.AMEX_BUSINESS;
import com.amex.uam.model.AMEX_UNIT;
import com.amex.uam.model.MS_USER_ADMIN;
import com.amex.uam.model.Message;
import com.amex.uam.model.USER_DEPT;
import com.amex.uam.service.AmexBusService;
import com.amex.uam.service.AmexUnitService;
import com.amex.uam.service.DeptService;
import com.amex.uam.service.GeoService;
import com.amex.uam.service.MstrService;
import com.amex.uam.service.NotifyService;
import com.amex.uam.service.NotifyService.Action;
import com.amex.uam.service.NotifyService.Product;
import com.amex.uam.service.UserService;
import com.amex.uam.util.Utils;
import com.microstrategy.web.beans.RequestKeys;
import com.microstrategy.web.objects.WebIServerSession;

@Controller("userAction")
public class UserAction {
	private UserService userService;
	private DeptService deptService;
	private AmexBusService amexBusService;
	private AmexUnitService amexUnitService;
	private GeoService geoService;
	private NotifyService notifyService;
	private MstrService mstrService;
	
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@Resource
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
	@Resource
	public void setAmexBusService(AmexBusService amexBusService) {
		this.amexBusService = amexBusService;
	}
	@Resource
	public void setAmexUnitService(AmexUnitService amexUnitService) {
		this.amexUnitService = amexUnitService;
	}
	@Resource
	public void setGeoService(GeoService geoService) {
		this.geoService = geoService;
	}
	@Resource
	public void setNotifyService(NotifyService notifyService) {
		this.notifyService = notifyService;
	}
	@Resource
	public void setMstrService(MstrService mstrService) {
		this.mstrService = mstrService;
	}
	
	public Message searchUser(String searchParam, String searchDBFldString, WebIServerSession session){
		return userService.searchUser(searchParam, searchDBFldString, session);
	}
	public Message searchUser(RequestKeys keys, WebIServerSession session) {
		String queryVal = keys.getValue("queryVal");
		String searchDBFldString = keys.getValue("searchDBFld");
		return searchUser(queryVal, searchDBFldString, session);
	}
	// load a user by amexWebId
	public Message loadUserByAmexWebId(RequestKeys keys){
		String amexWebId = keys.getValue("amexWebId");
		return loadUserByAmexWebId(amexWebId);
	}
	public Message loadUserByAmexWebId(String amexWebId){
		return userService.loadUserByAmexWebId(amexWebId);
	}
	/* Load user profile by userId */
	public Message loadUserProfile(String userId) {
		return userService.loadUserProfile(userId);
	}
	public Message loadUserProfile(RequestKeys keys) {
		String userId = keys.getValue("userId");
		return loadUserProfile(userId);
	}
	
	/* Load User Security Group */
	public Message loadUserSecurityGroup(RequestKeys keys) {
		String amexWebId = keys.getValue("amexWebId");
		return loadUserSecurityGroup(amexWebId);
	}
	public Message loadUserSecurityGroup(String amexWebId) {
		return userService.loadUserSecurityGroup(amexWebId);
	}
	
	public UserService getUserService() {
		return userService;
	}

	/*load GEO Country By AmexWebId*/
	public Message loadGEOCTRYByAmexWebId(RequestKeys keys) {
		String amexWebId = keys.getValue("amexWebId");
		return loadGEOCTRYByAmexWebId(amexWebId);
	}
	public Message loadGEOCTRYByAmexWebId(String amexWebId) {
		return userService.loadGEOCTRYByAmexWebId(amexWebId);
	}

	/*load GEO Region By AmexWebId*/
	public Message loadGEORGNByAmexWebId(RequestKeys keys) {
		String amexWebId = keys.getValue("amexWebId");
		return loadGEORGNByAmexWebId(amexWebId);
	}
	public Message loadGEORGNByAmexWebId(String amexWebId) {
		return userService.loadGEORGNByAmexWebId(amexWebId);
	}
	
	//update the Netezza based on User Profile General Information
	public void updateVUPUSRGenInfo(RequestKeys keys) {
		USER_DEPT ud = new USER_DEPT();
		ud.setDEPT_ID(keys.getValue("deptId"));
		ud.setDEPT_NM(keys.getValue("deptDesc"));
		updateVUPUSRDept(ud);
		
		AMEX_BUSINESS ab = new AMEX_BUSINESS();
		ab.setAMEX_BUS_CD(keys.getValue("bussUnitCD"));
		ab.setAMEX_BUS_DS_TX(keys.getValue("bussUnitDesc"));
		updateVUPUSRAmexBuss(ab);
		
		AMEX_UNIT au= new AMEX_UNIT();
		au.setAMEX_UNIT_CD(keys.getValue("amexUnitCD"));
		au.setAMEX_UNIT_DS_TX(keys.getValue("amexUnitDesc"));
		updateVUPUSRAmexUnit(au);
		
		MS_USER_ADMIN mua = new MS_USER_ADMIN();
		mua.setWORKFORCE_MBR_ID(keys.getValue("amexWebId"));
		mua.setDEPT_ID(keys.getValue("deptId"));
		mua.setAMEX_BUS_CD(keys.getValue("bussUnitCD"));
		//Department Description    -- deptDesc
		//Business Unit Description -- bussUnitDesc
		//AMEX Unit Description     -- amexUnitDesc
		mua.setUSER_EMAIL_AD_TX(keys.getValue("emailAddr"));
		mua.setWORKFORCE_MBR_MAN_1ST_NM(keys.getValue("mFName"));
		mua.setWORKFORCE_MBR_MAN_MI_TX(keys.getValue("mMidInit"));
		mua.setWORKFORCE_MBR_MAN_LST_NM(keys.getValue("mLName"));
		mua.setAMEX_UNIT_CD(keys.getValue("amexUnitCD"));
		mua.setCOST_CTR_CD(keys.getValue("costCenter"));
		mua.setUSER_CMPL_PHONE_NO(keys.getValue("phone"));
		mua.setWORKFORCE_MBR_MAN_EMAIL_AD_TX(keys.getValue("mgrEmailAddr"));
		mua.setVP_NM(keys.getValue("VPName"));
		mua.setACSS_RSN_TX(keys.getValue("reason4Accs"));
		mua.setNDA_TS(Utils.parseTimestamp(keys.getValue("accEndDate")));
		updateVUPUSRGenInfo(mua);
	}
	public int updateVUPUSRGenInfo(MS_USER_ADMIN mua){
		return userService.updateVUPUSRGenInfo(mua);
	}
	public boolean updateVUPUSRDept(USER_DEPT ud){
		return userService.updateVUPUSRDept(ud);
	}
	public int updateVUPUSRAmexBuss(AMEX_BUSINESS ab){
		return userService.updateVUPUSRAmexBuss(ab);
	}
	public int updateVUPUSRAmexUnit(AMEX_UNIT au){
		return userService.updateVUPUSRAmexUnit(au);
	}

	public Message updateVUPMSTRProd(RequestKeys keys) {
		String mstrProd = keys.getValue("mstrProdOut");
		String amexWebId= keys.getValue("amexWebId");
		try{
			return updateVUPMSTRProd(mstrProd, amexWebId);
		} catch (Throwable t) {
			t.printStackTrace();
			return new Message(Message.ERROR);
		}
	}
	public Message updateVUPMSTRProd(String msrtProd, String amexWebId) {
		return userService.updateVUPMSTRProd(msrtProd, amexWebId);
	}
	
	public Message updateUserType(RequestKeys keys){
		String userName = Utils.trim(keys.getValue("amexWebId"), null);
		String userTypeVal = Utils.trim(keys.getValue("usrTpRadio"), null);// modified : object
		String userTypeOri = Utils.trim(keys.getValue("ori_usrTpRadio"), null); // original : object
		try {
			return this.userService.updateUserGroup(userName, userTypeOri, userTypeVal);
		} catch (Throwable t) {
			t.printStackTrace();
			return new Message(Message.ERROR);
		}
	}
	
	public Message updateGMAPS(RequestKeys keys) {
		String userName = Utils.trim(keys.getValue("amexWebId"), null);
		String gmapsVal = Utils.trim(keys.getValue("gmapsRadio"), null);// modified : object
		String gmapsOri = Utils.trim(keys.getValue("ori_gmapsRadio"), null); // original : object
		
		System.out.println("GMAPS modified: " + gmapsVal);
		System.out.println("GMAPS original: " + gmapsOri);
		
		if ("-1".equals(gmapsVal)) {
			gmapsVal = null;
		}
		 
		try {
			return this.userService.updateUserGroup(userName, gmapsOri, gmapsVal);
		} catch (Throwable t) {
			t.printStackTrace();
			return new Message(Message.ERROR);
		}
	}

	public Message updateOptBlue(RequestKeys keys) {
		String userName = Utils.trim(keys.getValue("amexWebId"), null);
		String optBlueVal = Utils.trim(keys.getValue("optBlueRadio"), null);// modified : object : -1 indicates no access
		String optBlueOri = Utils.trim(keys.getValue("ori_optBlueRadio"), null); // original : object
		
		System.out.println("Opt Blue - modified: " + optBlueVal);
		System.out.println("Opt Blue - original: " + optBlueOri);
		
		
		if ("-1".equals(optBlueVal)) {
			optBlueVal = null;
		}
		 
		try {
			return this.userService.updateUserGroup(userName, optBlueOri, optBlueVal);
		} catch (Throwable t) {
			t.printStackTrace();
			return new Message(Message.ERROR);
		}
	}

	public Message updateMarcFuncGroup(RequestKeys keys, WebIServerSession session) {
		String amexWebId = Utils.trim(keys.getValue("amexWebId"), null);
		String empId = Utils.trim(keys.getValue("empId"), null);
		String marcFuncVal = Utils.trim(keys.getValue("marcFuncRadio"), null); // modified : list
		String marcFuncOri = Utils.trim(keys.getValue("ori_marcFuncRadio"), null); // original : list 
		
		return this.userService.updateMarcFuncGroup(empId, amexWebId, marcFuncOri, marcFuncVal, session);
	}
	
	public void updateRGNCTRY(RequestKeys keys) {
		String lifExcepCountry = keys.getValue("rcCtry");// country name list
		String lifExcepRegion = keys.getValue("rcRgn");// region name list
		String amexWebId= keys.getValue("amexWebId");
		System.out.println("LIF Exception - modified: " + lifExcepCountry);
		System.out.println("LIF Exception: " + lifExcepRegion);
		updateRGNCTRY(lifExcepCountry, lifExcepRegion, amexWebId);
	}
	public void updateRGNCTRY(String lifExcepCountry, String lifExcepRegion, String amexWebId){
		updateRGN(lifExcepRegion, amexWebId);
		updateCTRY(lifExcepCountry, amexWebId);
	}
	public Message updateRGN(String lifExcepRegion, String amexWebId){
		return userService.updateRGN(lifExcepRegion, amexWebId);
	}
	
	public Message updateCTRY(String lifExcepCountry, String amexWebId){
		return userService.updateCTRY(lifExcepCountry, amexWebId);
	}
	
	public void notifyApproved(RequestKeys keys) {
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("userId", Utils.trim(keys.getValue("userId"), ""));
		params.put("accessType", Utils.trim(keys.getValue("accType"), ""));
		params.put("costCenter", Utils.trim(keys.getValue("costCenter"), ""));
		params.put("reason", Utils.trim(keys.getValue("reason4Accs"), ""));
		params.put("middleInitial", Utils.trim(keys.getValue("midName"), ""));
		params.put("firstName", Utils.trim(keys.getValue("firstName"), ""));
		params.put("lastName", Utils.trim(keys.getValue("lastName"), ""));
		params.put("mFirstName", Utils.trim(keys.getValue("mFName"), ""));
		params.put("userEmail", Utils.trim(keys.getValue("emailAddr"), ""));
		params.put("managerEmail", Utils.trim(keys.getValue("mgrEmailAddr"), ""));
		
		String gmapsVal = Utils.trim(keys.getValue("gmapsRadio"), null);
		String optBlueVal = Utils.trim(keys.getValue("optBlueRadio"), null);
		
		List<Product> prod = new ArrayList<NotifyService.Product>();
		String mstrProdOut = Utils.trim(keys.getValue("mstrProdOut"), "");
		String[] arr = mstrProdOut.split(",");
		for (int i = 0; i < arr.length; i++) {
			int c = Utils.parseInt(arr[i], -1);
			switch (c) {
			case 1:
				prod.add(Product.MARC);
				break;
			case 2:
				prod.add(Product.Dashboard);
				break;
			case 3: 
				prod.add(Product.ICard);
				break;
			}
		}
		
		// Registration
		notifyService.asyncNotify(Action.Approve, prod, params);

		// OptBlue
		if (!"-1".equals(optBlueVal)){
			notifyService.asyncNotify(Action.OptBlue, prod, params);
		}
		
		// GMAPS
		if (!"-1".equals(gmapsVal)){
			notifyService.asyncNotify(Action.GMAPS, prod, params);	
		}
		
	}
	public Message removeUser(RequestKeys keys, WebIServerSession session) {
		String userIds = Utils.trim(keys.getValue("userIds"), null);
		Message msg = userService.removeUser(userIds);
		if (msg.getStatus() == Message.OK) {
			msg = mstrService.removeUser(userIds, session);
		}
		return msg;
	}
	
}